#include<cstdlib>
#include<iostream>
#include<cmath>
#include<complex>
#include<sstream>
#include<string>

#include<formic/fqmc/fqmc.h>
//#include<formic/fqmc/input.h>
//#include<formic/all_data.h>
//#include<formic/command/command.h>
//#include<formic/output/output.h>
//#include<formic/random/random.h>
//#include<formic/numeric/numeric.h>
//#include<formic/timing/timing.h>
//#include<formic/mpi/interface.h>
#include<formic/test_driver/utilities.h>
#include<formic/exception.h>

int main(int argc, char ** argv) {

  const int retval = formic::fqmc::main(argc, argv);
  if (retval != EXIT_SUCCESS)
    return retval;

  const double rhf_energy = -2.188928720406;
  const double exact_sample_energy = -1.487534285446;
  const double mc_sample_energy = -1.482026741448;
  const double fci_energy = -2.252972;
  const double opt_energy = -2.252921433997;

  std::string line;

//  // check psi3 hartree fock energy
//  {
//    std::ifstream ifs("output.dat");
//    if ( ! formic::test::get_line_containing_str(ifs, line, "* SCF total energy   =") )
//      throw formic::Exception("found no lines in the psi3 output file with the string \"%s\"") % "* SCF total energy   =";
//    ifs.close();
//    double val = 0.0;
//    std::stringstream ss(line);
//    ss >> line >> line >> line >> line >> line >> val;
//    if ( std::abs(rhf_energy - val) > 1.0e-9 )
//      throw formic::Exception("psi3 rhf energy is incorrect");
//  }

//  // check formic hartree fock energy
//  {
//    std::ifstream ifs("output.txt");
//    if ( ! formic::test::get_line_containing_str(ifs, line, "Hartree Fock energy =") )
//      throw formic::Exception("found no lines in the output file with the string \"%s\"") % "Hartree Fock energy =";
//    ifs.close();
//    double val = 0.0;
//    std::stringstream ss(line);
//    ss >> line >> line >> line >> line >> val;
//    if ( std::abs(rhf_energy - val) > 1.0e-9 )
//      throw formic::Exception("formic rhf energy is incorrect");
//  }

//  // check exact sampling energy
//  {
//    std::ifstream ifs("output.txt");
//    if ( ! formic::test::get_line_containing_str(ifs, line, "mean =") )
//      throw formic::Exception("found no lines in the output file with the string \"%s\"") % "mean =";
//    ifs.close();
//    double val = 0.0;
//    std::stringstream ss(line);
//    ss >> line >> line >> val;
//    if ( std::abs(exact_sample_energy - val) > 1.0e-9 )
//      throw formic::Exception("formic exact sampling energy is incorrect");
//  }

//  // check MC sampling energy
//  {
//    std::ifstream ifs("output.txt");
//    if ( ! formic::test::get_line_containing_str(ifs, line, "mean =") )
//      throw formic::Exception("found no lines in the output file with the string \"%s\"") % "mean =";
//    if ( ! formic::test::get_line_containing_str(ifs, line, "mean =") )
//      throw formic::Exception("found only one line in the output file with the string \"%s\"") % "mean =";
//    ifs.close();
//    double val = 0.0;
//    std::stringstream ss(line);
//    ss >> line >> line >> val;
//    if ( std::abs(mc_sample_energy - val) > 1.0e-9 )
//      throw formic::Exception("formic monte carlo sampling energy is incorrect");
//  }

  // check optimized energy
  {
    std::ifstream ifs("output.txt");
    while ( formic::test::get_line_containing_str(ifs, line, "mean =") ) {}
    ifs.close();
    double val = 0.0;
    std::stringstream ss(line);
    ss >> line >> line >> val;
    if ( std::abs(opt_energy - val) > 1.0e-6 )
      throw formic::Exception("formic optimized energy is incorrect");
  }

  return EXIT_SUCCESS;

}
